﻿using System;
using System.Collections;
using System.Data;
using System.Web.UI;
using System.Xml;
using AMS.Core.Entities;
using VitCode.BaseDataPageCreator;
using VitCode.BaseDataPageCreator.Setting;
using VitCode.Castle.DataAccess;
using VitCode.Castle.Helper;
using VitCode.Castle.Management;
using VitCode.Web.UI;
using VitCode.Web.UI.WebControls;

namespace AMS.Web.BaseData
{
    public partial class BarcodeFormatSet : Page
    {
        private BaseDataManager m_help = new BaseDataManager();
       private readonly CastleAccess m_Acc = new CastleAccess();
        private string sql = "";
        private ObjXmlReader m_XmlReader;
        PermissionManager m_PermissionManger = new PermissionManager();

        private string QueryCondition
        {
            get
            {

                if (ViewState["QueryCondition"] != null)
                    return ViewState["QueryCondition"].ToString();
                else
                    return "";
            }
            set { ViewState["QueryCondition"] = value; }
        }

        private string MenuId
        {
            get
            {

                if (ViewState["MenuId"] != null)
                    return ViewState["MenuId"].ToString();
                else
                    return "";
            }
            set { ViewState["MenuId"] = value; }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.Cookies["Code"] == null)
                {
                    Response.Redirect("../Default.aspx");
                }
                if (Request["MenuId"] != null)
                    MenuId = Request["MenuId"].Trim();

                if (!m_PermissionManger.CheckPageIsCanAdd(this))
                    btnAdd.Enabled = false;
             }
           
             ListGridInfo.DataSourceDelegate = new PaginationGrid.GridDataSourceDelegate(BindList);

        }

        private object BindList()
        {
            string xmlPath = AppDomain.CurrentDomain.BaseDirectory + "AssetQuery\\ProSetting.xml";
            m_XmlReader = new ObjXmlReader(xmlPath);
            XmlNode node = m_XmlReader.GetXmlNode("Setting/BarcodeFormatSql");
            if (node != null)
            {
                sql = m_XmlReader.GetNodeAttributeValue(node, "sql", true);
                sql += " " + QueryCondition;
            }
            DataTable dt = m_Acc.GetDataTableBySql(sql);


            return dt;
        }

        protected void ListGridInfo_OnGridItemSelect(object sender, PaginationGridItemSelectArgs e)
        {
            string val = e.SelectedDataKeyValue;
            Response.Redirect("BarcodeFormatEdit.aspx?MenuId=" + MenuId + "&Id=" + val,false);
           
            
        }

        protected void btnQuery_Click1(object sender, EventArgs e)
        {
            QueryCondition = QueryHelper.GetQueryCondition(ddlCondition.SelectedValue, txtValue.Text, rbtAmount.Checked);
            ListGridInfo.DataSourceDelegate = new PaginationGrid.GridDataSourceDelegate(BindList);
            ListGridInfo.BindGrid();
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            Response.Redirect("BarcodeFormatEdit.aspx?MenuId=" + MenuId, false);
        }

        protected void btnRealDelete_Click(object sender, EventArgs e)
        {
            string strDelVal = ListGridInfo.GetSelectedItem(1);
            if(strDelVal.Trim().Length == 0)
            {
                Alert.WebMessageBox(Page,"没有选择要删除的标签格式!");
                return;
                
            }
            string[] strDel = strDelVal.Split(',');

            for (int i = 0; i < strDel.Length; i++)
            {
                if (!string.IsNullOrEmpty(strDel[i]))
                {
                    IList objList = m_help.GetObjectList(new BarcodeFormat().GetType().Name, "Id", strDel[i]);
                    if (objList != null && objList.Count > 0)
                    {
                        var currentBarcodeFormat = (BarcodeFormat)objList[0];
                        var currentTypeName = currentBarcodeFormat.AssetTypeName;
                        IList AssetList = m_help.GetObjectList(new AssetInfo().GetType().Name, "TypeName", currentTypeName);
                        if (AssetList != null && AssetList.Count > 0)
                        {
                            Alert.MessageBox("此条码格式已经被资产使用，不能删除！");
                            return;
                        }
                        m_help.Delete(currentBarcodeFormat);
                    }
                }

            }
            ListGridInfo.DataSourceDelegate = new PaginationGrid.GridDataSourceDelegate(BindList);
            ListGridInfo.BindGrid();
        }
    }
}
